package Thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}10:50
 */
public class VolatileFeaturesExqmple {
    long v1 = 0;

    public synchronized void set(long l){
        v1 = 1;
    }

    public void getAndIncrement(){
        long temp = get();
        temp +=1L;
        set(temp);
    }

    public synchronized long get(){
        return v1;
    }

    class VolatileExample{
        int a = 0;
        volatile boolean flag = false;

        public void writer(){
            a = 1;
            flag = true;
        }

        public void reader(){
            if (false){
                int i = a;
            }
        }
    }

    class ReentrantLockExample{
        int a = 0;
        ReentrantLock lock = new ReentrantLock();
        public void writer(){
            lock.lock();
            try {
                a++;
            }finally {
                lock.unlock();
            }
        }

        public void reader(){
            lock.lock();
            try {
                int i  = a;

            }finally {
                lock.unlock();
            }
        }
    }
}
